package bulk_param.numspe.controller;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;

import bulk_param.dao.InactivationPointDao;
import bulk_param.modele.InactivationPointModel;
import bulk_param.view.NumSpe_Inactive_View;


public class ButtonNumSpeInactiveController implements ActionListener{
	
	private NumSpe_Inactive_View numSpe_Inactive_View;
	private InactivationPointModel inactivationPointModel;
	private InactivationPointDao inactivationPointDao;
	
	
	
	public ButtonNumSpeInactiveController(NumSpe_Inactive_View numSpe_Inactive_View){
		this.numSpe_Inactive_View=numSpe_Inactive_View;
		inactivationPointModel = new InactivationPointModel();
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		
		if(arg0.getSource().equals(numSpe_Inactive_View.btInactiver)){
			System.out.println(" Access Numeros Speciaux inactivation de Point ZABPQ ");
			//Remise des contours champs à gris	
			numSpe_Inactive_View.txtZABPQ.setBorder(BorderFactory.createLineBorder(Color.lightGray));
			numSpe_Inactive_View.dateChooser.setBorder(BorderFactory.createLineBorder(Color.lightGray));
			
			// TYPE DE TRAFIC NATIONAL
				if(!numSpe_Inactive_View.txtZABPQ.getText().isEmpty()
					&& numSpe_Inactive_View.dateChooser.getDate() != null){
					if(!numSpe_Inactive_View.txtZABPQ.getText().matches(inactivationPointModel.getZABPQ_REGEX())){
						numSpe_Inactive_View.txtZABPQ.setBorder(BorderFactory.createLineBorder(Color.red));
						JOptionPane.showMessageDialog(null,inactivationPointModel.getZABPQ_REGEX_MESSAGE(),"Attention",JOptionPane.WARNING_MESSAGE);										
					}else{
						//Verification si le point existe ou pas
						inactivationPointDao = new InactivationPointDao();
						boolean isExist = inactivationPointDao.isPointExist("33",numSpe_Inactive_View.txtZABPQ.getText().substring(1));
						
						if(isExist == false){
							String msg = "Le point : 33" + numSpe_Inactive_View.txtZABPQ.getText().substring(1) + "\nn'éxiste pas";
			                JOptionPane.showMessageDialog(null, msg, "Message", JOptionPane.INFORMATION_MESSAGE);							
						}
						else{	
							try {
								if(inactivationPointDao.isDateBefore("33",numSpe_Inactive_View.txtZABPQ.getText().substring(1),numSpe_Inactive_View.dateChooser.getDate())==false){
									Timestamp inBase= inactivationPointDao.dateInBase("33", numSpe_Inactive_View.txtZABPQ.getText().substring(1));
									String pointZABPQ= "33"+numSpe_Inactive_View.txtZABPQ.getText().substring(1);
									Date date= numSpe_Inactive_View.dateChooser.getDate();
									inactivationPointDao.inactivePointZABPQ(pointZABPQ,date,inBase);
									String succesMsg= "Le point : " +33+ numSpe_Inactive_View.txtZABPQ.getText().substring(1) + "\na été inactivé avec succès";
									JOptionPane.showMessageDialog(null, succesMsg, "Message", JOptionPane.INFORMATION_MESSAGE);	
								}else{
									Timestamp date= inactivationPointDao.dateInBase("33", numSpe_Inactive_View.txtZABPQ.getText().substring(1));
									String dateMsg="Vous devez choisir une date \négale ou postérieure à la date : "+new java.sql.Date(date.getTime());
									JOptionPane.showMessageDialog(null, dateMsg, "Attention", JOptionPane.WARNING_MESSAGE);	
								}					
							} catch (ParseException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						inactivationPointDao.closeConnection();
					}					
				}else if(numSpe_Inactive_View.txtZABPQ.getText().isEmpty()
						|| numSpe_Inactive_View.dateChooser.getDate() == null){
					if (numSpe_Inactive_View.txtZABPQ.getText().isEmpty()) {
					JOptionPane.showMessageDialog(null,inactivationPointModel.getZABPQ_REGEX_MESSAGE(),"Attention",JOptionPane.WARNING_MESSAGE);
						numSpe_Inactive_View.txtZABPQ.setBorder(BorderFactory.createLineBorder(Color.red));
						System.out.println("INACTIVATION : Point ZABPQ vide");
					}
					if(numSpe_Inactive_View.dateChooser.getDate() == null){
					JOptionPane.showMessageDialog(null,inactivationPointModel.getDATE_REGEX_MESSAGE(),"Attention",JOptionPane.WARNING_MESSAGE);
					numSpe_Inactive_View.dateChooser.setBorder(BorderFactory.createLineBorder(Color.red));
					System.out.println("INACTIVATION : La date n'est pas renseigné");
				}
			}
		}		
	}
}


